home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / init.c < prev    next >
C/C++ Source or Header  |  1993-07-27  |  18KB  |  515 lines

  1. /*
  2.  * init.c   -- Initialisation functions for GSVIEW.EXE,
  3.  *             a graphical interface for MS-Windows Ghostscript
  4.  * Copyright (C) 1993  Russell Lang
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   Author: Russell Lang
  21.  * Internet: rjl@monu1.cc.monash.edu.au
  22.  */
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <mmsystem.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <dir.h>
  35. #include <io.h>
  36. #define NeedFunctionPrototypes 1
  37. #include "ps.h"
  38. #include "gsview.h"
  39.  
  40. /* Open/Save File Dialog Box */
  41. OPENFILENAME ofn;
  42. char szOFilename[MAXSTR];    /* filename for OFN */
  43. char szOFilter[256];        /* filter for OFN */
  44. /* buttons */
  45. WNDPROC lpfnButtonWndProc;    /* default button WndProc */
  46. struct buttonlist {
  47.    HWND hbutton;
  48.    struct buttonlist *next;
  49. };
  50. struct buttonlist *buttonhead, *buttontail;
  51. int real_button_width;
  52.  
  53. /* Don't start another instance - use previous instance */
  54. void
  55. gsview_init0(LPSTR lpszCmdLine)
  56. {
  57.     HWND hwnd = FindWindow(szClassName, szAppName);
  58.     BringWindowToTop(hwnd);
  59.     if (lstrlen(lpszCmdLine) != 0) {
  60.         /* open file specified on command line */
  61.         LPSTR szFile = GlobalAllocPtr(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  62.         if (szFile) {
  63.         lstrcpy(szFile, lpszCmdLine);
  64.         PostMessage(hwnd, WM_COMMAND, IDM_DROP, (LPARAM)szFile);
  65.         }
  66.     }
  67. }
  68.  
  69. /* main initialisation */
  70. void
  71. gsview_init1(LPSTR lpszCmdLine)
  72. {
  73. WNDCLASS wndclass;
  74. WORD version = LOWORD(GetVersion());
  75. char *p;
  76. char workdir[MAXSTR];
  77. char filedir[MAXSTR];
  78. int length = 64;
  79.  
  80.     while (length && !SetMessageQueue(length))
  81.         length--;    /* reduce size and try again */
  82.     if (length == 0)
  83.         exit(0);    /* panic */
  84.     
  85.     if ((LOBYTE(version)<<8) + HIBYTE(version) >= 0x30a)
  86.         is_win31 = TRUE;
  87.  
  88.     /* get path to help file */
  89.     GetModuleFileName(phInstance, szHelpName, sizeof(szHelpName));
  90.     if ((p = strrchr(szHelpName,'\\')) != (char *)NULL)
  91.         p++;
  92.     else
  93.         p = szHelpName;
  94.     LoadString(phInstance, IDS_HELPFILE, p, sizeof(szHelpName) - (p-szHelpName));
  95.  
  96.     /* help message for GetOpenFileName Dialog Box */
  97.     help_message = RegisterWindowMessage(HELPMSGSTRING);
  98.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  99.     
  100.     /* register the window class */
  101.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  102.     wndclass.lpfnWndProc = WndImgProc;
  103.     wndclass.cbClsExtra = 0;
  104.     wndclass.cbWndExtra = sizeof(LONG);
  105.     wndclass.hInstance = phInstance;
  106.     wndclass.hIcon = LoadIcon(phInstance,"gsview");
  107.     wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  108.     wndclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  109.     wndclass.lpszMenuName = NULL;
  110.     wndclass.lpszClassName = szClassName;
  111.     RegisterClass(&wndclass);
  112.  
  113.     /* defaults if entry not in gsview.ini */
  114.     strcpy(szGSwin, DEFAULT_GSCOMMAND);
  115.     img_origin.x = img_origin.y = CW_USEDEFAULT;
  116.     img_size.x = img_size.y = CW_USEDEFAULT;
  117.     xdpi = ydpi = DEFAULT_RESOLUTION;
  118.     button_show = TRUE;
  119.     epsf_clip = FALSE;
  120.     epsf_warn = FALSE;
  121.     media = IDM_LETTER;
  122.     quick = TRUE;
  123.         redisplay = FALSE;
  124.     safer = TRUE;
  125.     save_dir = TRUE;
  126.     settings = TRUE;
  127.     timeout = DEFAULT_TIMEOUT;
  128.     orientation = IDM_PORTRAIT;
  129.     swap_landscape = FALSE;
  130.     hmenu = LoadMenu(phInstance, "gsview_menu");
  131.     GetMenuString(hmenu, media, medianame, sizeof(medianame), MF_BYCOMMAND);
  132.     haccel = LoadAccelerators(phInstance, "gsview_accel");
  133.     getcwd(workdir, sizeof(workdir));
  134.     /* read entries from gsview.ini */
  135.     read_profile();
  136.  
  137.     hwndimg = CreateWindow(szClassName, (LPSTR)szAppName,
  138.           WS_OVERLAPPEDWINDOW,
  139.           img_origin.x, img_origin.y, 
  140.           img_size.x, img_size.y, 
  141.           NULL, NULL, phInstance, (void FAR *)NULL);
  142.  
  143.     /* load DLL for sounds */
  144.     if (is_win31) {
  145.         /* MMSYSTEM.DLL requires Windows 3.1, so to allow gsview to run
  146.            under Windows 3.0 we can't use the import library */
  147.         hlib_mmsystem = LoadLibrary("MMSYSTEM.DLL");
  148.         if (hlib_mmsystem >= HINSTANCE_ERROR) {
  149.         lpfnSndPlaySound = (FPSPS)GetProcAddress(hlib_mmsystem, "sndPlaySound");
  150.         }
  151.         else {
  152.         gserror(IDS_SOUNDNOMM, NULL, MB_ICONEXCLAMATION, -1);
  153.         hlib_mmsystem = (HINSTANCE)NULL;
  154.         }
  155.     }
  156.  
  157.     if (lstrlen(lpszCmdLine) >= 2) {
  158.         if ( ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-'))
  159.          &&  ((lpszCmdLine[1] == 'D') || (lpszCmdLine[1] == 'd')) ) {
  160.         debug = TRUE;
  161.         lpszCmdLine += 2;
  162.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  163.             lpszCmdLine++;
  164.         }
  165.      }
  166.     if (lstrlen(lpszCmdLine) != 0) {
  167.         /* open file specified on command line */
  168.         LPSTR szFile = GlobalAllocPtr(GHND, lstrlen(lpszCmdLine)+1);
  169.         if (szFile) {
  170.         lstrcpy(szFile, lpszCmdLine);
  171.         PostMessage(hwndimg, WM_COMMAND, IDM_DROP, (LPARAM)szFile);
  172.         }
  173.         /* ignore last saved directory */
  174.         /* use directory of file if given, or work directory */
  175.         if ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-')) {
  176.         lpszCmdLine += 2;
  177.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  178.             lpszCmdLine++;
  179.         }
  180.         lstrcpy(filedir, lpszCmdLine);
  181.         if ( (p = strrchr(filedir, '\\')) == (char *)NULL ) {
  182.             if ( (p = strrchr(filedir, ':')) == (char *)NULL )
  183.             strcpy(filedir, workdir);  /* no path so use work directory */
  184.         else
  185.             *(++p) = '\0';
  186.         }
  187.         else
  188.         *(++p) = '\0';
  189.         if (!((strlen(filedir)==2) && isalpha(filedir[0]) && (filedir[1]==':')))
  190.             chdir(filedir);
  191.         if (isalpha(filedir[0]) && (filedir[1]==':'))
  192.         (void) setdisk(toupper(filedir[0])-'A');
  193.     }
  194.     play_sound(SOUND_START);
  195. }
  196.  
  197. /* create gsview window menu bar and buttons */
  198. void
  199. gsview_create()
  200. {
  201. int i;
  202. char cReplace;
  203. WNDCLASS wndclass;
  204. HGLOBAL hglobal;
  205. int FAR *pButtonID;
  206. TEXTMETRIC tm;
  207. HDC hdc;
  208. HWND hbutton;
  209. WNDPROC    lpfnMenuButtonProc;
  210. POINT char_size;        /* size of default text characters */
  211. POINT button_size, button_shift;
  212.  
  213.     /* setup OPENFILENAME struct */
  214.     if (!LoadString(phInstance, IDS_FILTER, szOFilter, sizeof(szOFilter)-1))
  215.         return;
  216.     cReplace = szOFilter[strlen(szOFilter)-1];
  217.     for (i=0; szOFilter[i] != '\0'; i++)
  218.         if (szOFilter[i] == cReplace)
  219.         szOFilter[i] = '\0';
  220.     ofn.lStructSize = sizeof(OPENFILENAME);
  221.     ofn.hwndOwner = hwndimg;
  222.     ofn.lpstrFilter = szOFilter;
  223.     ofn.nFilterIndex = FILTER_PS;
  224.     ofn.lpstrFile = szOFilename;
  225.     ofn.nMaxFile = sizeof(szOFilename);
  226.     ofn.lpstrFileTitle = (LPSTR)NULL;
  227.     ofn.nMaxFileTitle = 0;
  228.     ofn.lpstrTitle = (LPSTR)NULL;
  229.     ofn.lpstrInitialDir = (LPSTR)NULL;
  230.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_SHOWHELP;
  231.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  232.  
  233.     /* add menu to image window */
  234.     SetMenu(hwndimg, hmenu);
  235.  
  236.     /* get default text size */
  237.     hdc = GetDC(hwndimg);
  238.     GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  239.     ReleaseDC(hwndimg,hdc);
  240.     char_size.x = tm.tmAveCharWidth;
  241.     char_size.y = tm.tmHeight;
  242.  
  243.     /* set size of info area, buttons and offset to child window */
  244.     info_rect.left = 0;
  245.     info_rect.right = info_rect.left + 60 * char_size.x;
  246.     info_rect.top = 0;
  247.     info_rect.bottom = char_size.y;
  248.     button_size.x = 24;
  249.     button_size.y = 24;
  250.     button_shift.x = 0;
  251.     button_shift.y = button_size.y - 1;
  252.     button_rect.top = info_rect.bottom;
  253.     button_rect.left = -1;
  254.     button_rect.right = button_size.x - 2;
  255.     button_rect.bottom = 0;        /* don't care */
  256.     real_button_width = button_rect.right;
  257.     if (!button_show)
  258.         button_rect.right = 0;
  259.     img_offset.x = button_rect.right + (button_show ? 1 : 0);
  260.     img_offset.y = info_re